package controller

import (
	"gin/response"
	"gin/service"
	"gin/types"
	"github.com/gin-gonic/gin"
)

var (
	financeService = service.Finance{}
)

type Finance struct {
}

// FinanceHourCount 获取租户办公地点
func (c *Finance) FinanceHourCount(ctx *gin.Context) {

	var req types.FinanceHourCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	resData, queryErr := financeService.FinanceHourCount(tenantId, req.Month, req.ProjectIdList, req.EpisodeList)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// FinanceAssetHourCount 获取租户办公地点
func (c *Finance) FinanceAssetHourCount(ctx *gin.Context) {

	var req types.FinanceHourCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	locked, err := financeService.FinanceAssetHourCountIsLocked(req.Month, req.TaskType)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	var resData interface{}
	if locked {
		resData, err = financeService.FinanceAssetHourCountByRecord(req.TaskType, req.Month)
	} else {
		resData, err = financeService.FinanceAssetHourCountV2(tenantId, req.TaskType, req.Month, req.ProjectIdList, req.EpisodeList)
	}
	if err != nil {
		// 查询失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 财务报表v3
func (c *Finance) FinanceAssetHourCountV3(ctx *gin.Context) {

	var req types.FinanceHourCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")

	var resData interface{}
	resData, err = financeService.FinanceAssetHourCountV3(ctx, tenantId, req.TaskType, req.Month, req.ProjectIdList, req.EpisodeList)

	if err != nil {
		// 查询失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// FinanceAssetHourCountLock 锁定工时报表
func (c *Finance) FinanceAssetHourCountLock(ctx *gin.Context) {

	var req types.FinanceHourCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	err = financeService.FinanceAssetHourCountLockRecord(tenantId, req.TaskType, req.Month, req.ProjectIdList, req.EpisodeList)

	if err != nil {
		// 查询失败
		response.Fail(ctx, nil, err.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": 0}, "success")
	}
}
